package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.shoupiaoyuan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdkj.common.core.constant.SecurityConstants;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.dto.SwMessageDTO;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwAuCenterConductorAmountDifference;
import com.zdkj.subway.check.entity.SwAuConductorHierarchyAmountDifference;
import com.zdkj.subway.check.enums.MessageEnums;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.fegin.SupportService;
import com.zdkj.subway.check.mapper.SwAuCenterConductorAmountDifferenceMapper;
import com.zdkj.subway.check.mapper.SwAuConductorHierarchyAmountDifferenceMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.util.DateFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 售票员票款层级差异
 */
@Slf4j
@RestController
@RequestMapping("/check")
public class SwAuConductorHierarchyAmountDifferenceController {

    @Autowired
    private SupportService supportService;
    @Resource
    SwAuConductorHierarchyAmountDifferenceMapper swAuConductorHierarchyAmountDifferenceMapper;
    /**
     * 售票员差异审核
     */
    @Resource
    SwAuCenterConductorAmountDifferenceMapper swAuCenterConductorAmountDifferenceMapper;
    /**
     * 列表查询
     * @param page
     * @param swAuCenterConductorAmountDifference
     * @param param
     * @return
     */
    @RequestMapping("/getSwAuConductorHierarchyAmountDifferenceList")
    @ApiOperation(value = "列表查询", notes = "列表查询")
    public Page getSwAuConductorHierarchyAmountDifferenceList(Page page, SwAuCenterConductorAmountDifference swAuCenterConductorAmountDifference, @RequestParam Map<String,String> param){
        List<Map<String,Object>> list=swAuConductorHierarchyAmountDifferenceMapper.getSwAuConductorHierarchyAmountDifferenceList(param);
        page.setData(list,true);
        return page;
    }

    /**
     * 售票员层级差异协查替换
     * @param ids
     * @return
     */
    @RequestMapping("/replaceShoupiaoyuanCJCYDXC")
    @ApiOperation(value = "售票员层级差异协查替换", notes = "售票员层级差异协查替换")
    @Transactional(rollbackFor = Exception.class)
    public Result replaceShoupiaoyuanCJCYDXC(String ids){
        Result re=new Result(ResultDetailMsg.OK);
        try {
            QueryWrapper<SwAuConductorHierarchyAmountDifference> queryWrapper=new QueryWrapper<>();
            queryWrapper.in("id",ids.split(","));
            //先查询售票员票款层级差异
            List<SwAuConductorHierarchyAmountDifference> swAuConductorHierarchyAmountDifferences = swAuConductorHierarchyAmountDifferenceMapper.selectList(queryWrapper);
            for (SwAuConductorHierarchyAmountDifference swAuConductorHierarchyAmountDifference : swAuConductorHierarchyAmountDifferences) {
                QueryWrapper<SwAuCenterConductorAmountDifference> centerConductorAmountDifferenceQueryWrapper = new QueryWrapper<>();
                centerConductorAmountDifferenceQueryWrapper.eq("LINE_CODE", swAuConductorHierarchyAmountDifference.getLineCode());
                centerConductorAmountDifferenceQueryWrapper.eq("STATION_CODE", swAuConductorHierarchyAmountDifference.getStationCode());
                centerConductorAmountDifferenceQueryWrapper.eq("RECORD_TIME", new SimpleDateFormat("yyyy-MM-dd").format(swAuConductorHierarchyAmountDifference.getSettleTime()));
                centerConductorAmountDifferenceQueryWrapper.eq("CONDUCTOR_JOB_NO", swAuConductorHierarchyAmountDifference.getConductorPersonCode());
                centerConductorAmountDifferenceQueryWrapper.eq("EQUIPMENT_NUMBER", swAuConductorHierarchyAmountDifference.getEquipmentNumber());
                centerConductorAmountDifferenceQueryWrapper.eq("INVALID_STATUS", "0");
                centerConductorAmountDifferenceQueryWrapper.eq("DEL_FLAG", "0");
                SwAuCenterConductorAmountDifference swAuCenterConductorAmountDifference = swAuCenterConductorAmountDifferenceMapper.selectOne(centerConductorAmountDifferenceQueryWrapper);
                //审核状态 已审核，不能再做一键替换
                if(swAuCenterConductorAmountDifference.getAuditState().equals("1")){
                    re.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
                    re.setMsg("收入已审核，不能再做一键替换");
                    return re;
                }
            }
            //一键替换
            for (SwAuConductorHierarchyAmountDifference swAuConductorHierarchyAmountDifference : swAuConductorHierarchyAmountDifferences) {
                QueryWrapper<SwAuCenterConductorAmountDifference> centerConductorAmountDifferenceQueryWrapper = new QueryWrapper<>();
                centerConductorAmountDifferenceQueryWrapper.eq("LINE_CODE", swAuConductorHierarchyAmountDifference.getLineCode());
                centerConductorAmountDifferenceQueryWrapper.eq("STATION_CODE", swAuConductorHierarchyAmountDifference.getStationCode());
                centerConductorAmountDifferenceQueryWrapper.eq("RECORD_TIME", new SimpleDateFormat("yyyy-MM-dd").format(swAuConductorHierarchyAmountDifference.getSettleTime()));
                centerConductorAmountDifferenceQueryWrapper.eq("CONDUCTOR_JOB_NO", swAuConductorHierarchyAmountDifference.getConductorPersonCode());
                centerConductorAmountDifferenceQueryWrapper.eq("EQUIPMENT_NUMBER", swAuConductorHierarchyAmountDifference.getEquipmentNumber());
                centerConductorAmountDifferenceQueryWrapper.eq("INVALID_STATUS", "0");
                centerConductorAmountDifferenceQueryWrapper.eq("DEL_FLAG", "0");
                SwAuCenterConductorAmountDifference swAuCenterConductorAmountDifference = swAuCenterConductorAmountDifferenceMapper.selectOne(centerConductorAmountDifferenceQueryWrapper);
                //回馈补票金额
                BigDecimal replyFaretkAmount = swAuConductorHierarchyAmountDifference.getReplyFaretkAmount();
                //回馈充值金额
                BigDecimal replyInvestAmount = swAuConductorHierarchyAmountDifference.getReplyInvestAmount();
                //回馈售票金额
                BigDecimal replySellAmount = swAuConductorHierarchyAmountDifference.getReplySellAmount();
                //系统数据金额
                BigDecimal systemAmount = replyFaretkAmount.add(replyInvestAmount).add(replySellAmount);
                //差异金额
                BigDecimal diff_money = swAuCenterConductorAmountDifference.getPaidAmount().subtract(systemAmount).subtract(swAuCenterConductorAmountDifference.getPassengerWorkAmount()).subtract(swAuCenterConductorAmountDifference.getMarketTicketAmount()).subtract(swAuCenterConductorAmountDifference.getSingleTicketAmount()).subtract(swAuCenterConductorAmountDifference.getEmergencyAmount());

                //数据来源区分 sc、lc、acc
                if (swAuCenterConductorAmountDifference.getSysDataSource().equals("1")) { //sc
                    swAuCenterConductorAmountDifference.setScFaretkAmount(replyFaretkAmount);
                    swAuCenterConductorAmountDifference.setScSellAmount(replySellAmount);
                    swAuCenterConductorAmountDifference.setScInvestAmount(replyInvestAmount);
                    swAuCenterConductorAmountDifference.setSysAmount(systemAmount);
                    swAuCenterConductorAmountDifference.setDifferenceAmount(diff_money);
                } else if (swAuCenterConductorAmountDifference.getSysDataSource().equals("2")) {//lc
                    swAuCenterConductorAmountDifference.setLcFaretkAmount(replyFaretkAmount);
                    swAuCenterConductorAmountDifference.setLcSellAmount(replySellAmount);
                    swAuCenterConductorAmountDifference.setLcInvestAmount(replyInvestAmount);
                    swAuCenterConductorAmountDifference.setSysAmount(systemAmount);
                    swAuCenterConductorAmountDifference.setDifferenceAmount(diff_money);
                } else if (swAuCenterConductorAmountDifference.getSysDataSource().equals("3")) {//acc
                    swAuCenterConductorAmountDifference.setAccFaretkAmount(replyFaretkAmount);
                    swAuCenterConductorAmountDifference.setAccSellAmount(replySellAmount);
                    swAuCenterConductorAmountDifference.setAccInvestAmount(replyInvestAmount);
                    swAuCenterConductorAmountDifference.setSysAmount(systemAmount);
                    swAuCenterConductorAmountDifference.setDifferenceAmount(systemAmount);
                    swAuCenterConductorAmountDifference.setDifferenceAmount(diff_money);
                }else{

                }
                swAuConductorHierarchyAmountDifference.setReplaceStatus("1");
                swAuConductorHierarchyAmountDifference.setReplaceTime(new Date());
                swAuConductorHierarchyAmountDifferenceMapper.updateById(swAuConductorHierarchyAmountDifference); //替换状态
                swAuCenterConductorAmountDifferenceMapper.updateById(swAuCenterConductorAmountDifference);//替换金额
            }
        }catch (Exception e){
            log.error("{}",e);
            re.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            re.setMsg("替换失败");
        }
        return re;
    }

    /**
     * 下发
     * @param ids
     * @return
     */
    @RequestMapping("/sendStatusSwAuConductorHierarchyAmountDifference")
    @ApiOperation(value = "下发", notes = "下发")
    public Result sendStatusSwAuConductorHierarchyAmountDifference(String ids){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            for (String id : ids.split(",")) {
                //下发 更新下发状态
                SwAuConductorHierarchyAmountDifference swAuConductorHierarchyAmountDifference = swAuConductorHierarchyAmountDifferenceMapper.selectById(Integer.parseInt(id));
                swAuConductorHierarchyAmountDifference.setAssistState("2");
                swAuConductorHierarchyAmountDifference.setAssistTime(new Date());
                swAuConductorHierarchyAmountDifference.setJdReplyStatus("0");//给个默认状态
                swAuConductorHierarchyAmountDifference.setQfReplyStatus("0");//清分默认未回馈
                swAuConductorHierarchyAmountDifferenceMapper.updateById(swAuConductorHierarchyAmountDifference);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("下发失败");
        }
        return res;
    }

    /**
     * 加入协查
     * @param ids
     * @return
     */
    @RequestMapping("/addAssistSwAuConductorHierarchyAmountDifference")
    @ApiOperation(value = "加入协查", notes = "加入协查")
    public Result addAssistSwAuConductorHierarchyAmountDifference(String ids){
        PigxUser user = SecurityUtils.getUser();
        Result res=new Result(ResultDetailMsg.OK);
        try {
            for (String id : ids.split(",")) {
                SwAuConductorHierarchyAmountDifference swAuConductorHierarchyAmountDifference = swAuConductorHierarchyAmountDifferenceMapper.selectById(Integer.parseInt(id));
                swAuConductorHierarchyAmountDifference.setIsAddAssist("1");//已加入协查
                swAuConductorHierarchyAmountDifference.setAssistState("2");//协查状态2代表协查中
                swAuConductorHierarchyAmountDifference.setJdReplyStatus("0");//机电回馈状态
                swAuConductorHierarchyAmountDifference.setQfReplyStatus("0");//清分回馈状态
                swAuConductorHierarchyAmountDifference.setAssistTime(new Date());
                swAuConductorHierarchyAmountDifference.setSendPersonCode(user.getUserCode());//下发人工号
                swAuConductorHierarchyAmountDifference.setSendPersonName(user.getNikeName());//下发人
                swAuConductorHierarchyAmountDifferenceMapper.updateById(swAuConductorHierarchyAmountDifference);
            }
            SwMessageDTO swMessageDTO = new SwMessageDTO();
            swMessageDTO.setTitle(" BOM层级差异协查");
            swMessageDTO.setMsgType("协查");
            swMessageDTO.setCreatorName(user.getNikeName());
            swMessageDTO.setCreatorId(user.getId());
            swMessageDTO.setUrl(MessageEnums.MessageUrlEnums.JD_BOM_SC_LC_URL.getUrl());
            swMessageDTO.setRoleCode(MessageEnums.SBBJSRY_ROLE_CODE.getCode().toString());
            swMessageDTO.setContent("车站提交，站长待审核");
            supportService.saveInner(swMessageDTO, SecurityConstants.FROM_IN);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("下发失败");
        }
        return res;
    }
}
